﻿using Apps.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.DAL.Base
{
    public class PublicOperationRepository
    {
        public DBContainer db = new DBContainer();
        public virtual List<P_QRTZ_GetJobExecuteTime_Result> P_QRTZ_GetJobExecuteTime(string id)
        {
            return db.QRTZ_TRIGGERS.Where(m => m.TRIGGER_NAME == id).Select(m => new P_QRTZ_GetJobExecuteTime_Result { START_TIME = m.START_TIME }).ToList();
        }

        public virtual List<P_Sys_GetModule_RoleRight_Result> P_Sys_GetModule_RoleRight(string moduleId)
        {
            return new List<P_Sys_GetModule_RoleRight_Result>();
        }

        public virtual List<P_JOB_GetUnTrackTask_Result> P_JOB_GetUnTrackTask()
        {
            return new List<P_JOB_GetUnTrackTask_Result>();
        }

        public virtual List<P_Sys_GetModule_UserRight_Result> P_Sys_GetModule_UserRight(string moduleId)
        {
            return new List<P_Sys_GetModule_UserRight_Result>();
        }

        public virtual List<P_Sys_GetRightByRole_Result> P_Sys_GetRightByRole(string roleId)
        {
            return new List<P_Sys_GetRightByRole_Result>();
        }

        public virtual List<P_Sys_GetRightByUser_Result> P_Sys_GetRightByUser(string userId)
        {
            return new List<P_Sys_GetRightByUser_Result>();
        }

        public virtual List<P_Sys_GetRightOperate_Result> P_Sys_GetRightOperate(string userId, string url)
        {
            return new List<P_Sys_GetRightOperate_Result>();
        }

        public virtual List<P_Sys_GetRoleByUserId_Result> P_Sys_GetRoleByUserId(string userId)
        {
            return new List<P_Sys_GetRoleByUserId_Result>();
        }

        public virtual List<P_Sys_GetUserByDepId_Result> P_Sys_GetUserByDepId(string depId)
        {
            return new List<P_Sys_GetUserByDepId_Result>();
        }

        public virtual List<P_Sys_GetUserByRoleId_Result> P_Sys_GetUserByRoleId(string roleId, string depId)
        {
            return new List<P_Sys_GetUserByRoleId_Result>();
        }

        public virtual List<Nullable<int>> P_Sys_GetUserCountByDepId(string depId)
        {
            return new List<Nullable<int>>();
        }


        public virtual List<P_Sys_WebPart_Result> P_WebPart_GetInfo(Nullable<int> rows, string userId)
        {
            return new List<P_Sys_WebPart_Result>();
        }

        public virtual List<MIS_WebIM_Message> P_MIS_GetMessage(string user, Nullable<int> pageno, Nullable<int> rows, System.Data.Entity.Core.Objects.ObjectParameter rowscount)
        {
            return new List<MIS_WebIM_Message>();
        }

        public virtual List<MIS_WebIM_Message> P_MIS_GetMessage(string user, Nullable<int> pageno, Nullable<int> rows, System.Data.Entity.Core.Objects.ObjectParameter rowscount, System.Data.Entity.Core.Objects.MergeOption mergeOption)
        {
            return new List<MIS_WebIM_Message>();
        }

        public virtual List<P_MIS_GetSenderByReceiver_Result> P_MIS_GetSenderByReceiver(string receiver)
        {
            return new List<P_MIS_GetSenderByReceiver_Result>();
        }

        public virtual List<P_Sys_GetAllUsers_Result> P_Sys_GetAllUsers()
        {
            return new List<P_Sys_GetAllUsers_Result>();
        }

        public virtual List<P_WC_GetResponseContent_Result> P_WC_GetResponseContent(string officalAccountId, string matchKey)
        {
            return new List<P_WC_GetResponseContent_Result>();
        }

        public virtual List<Nullable<int>> P_Spl_GetWareCountByCategoryId(string categoryId)
        {
            return new List<Nullable<int>>();
        }

        public virtual List<P_Sys_GetRightDataByRoleAndModule_Result> P_Sys_GetRightDataByRoleAndModule(string roleId, string moduleId)
        {

            var SysRightList = from a in this.db.SysModuleDataFilter
                               join b_1 in (from ca in this.db.SysRightOperate
                                            from cb in this.db.SysRight
                                            from c in this.db.SysModuleDataFilter
                                            where (from d in this.db.SysRight where d.RoleId == roleId && d.ModuleId == moduleId select d.Id).Contains(ca.RightId)
                                            && ca.RightId == cb.Id && cb.ModuleId == c.ModuleId && ca.KeyCode == c.KeyCode
                                            select new { c.Id, ca.IsValid }
                                            ) on a.Id equals b_1.Id into joinb
                               from b in joinb.DefaultIfEmpty()
                               where a.ModuleId == moduleId
                               select new P_Sys_GetRightDataByRoleAndModule_Result
                               {
                                   Id = a.Id,
                                   Name = a.Name,
                                   KeyCode = a.KeyCode,
                                   ModuleId = a.ModuleId,
                                   Sort = a.Sort,
                                   isvalid = (b == null ? false : b.IsValid),
                                   RightId = roleId + moduleId
                               };


            return SysRightList.ToList();
        }

        public virtual List<P_Sys_GetRightByRoleAndModule_Result> P_Sys_GetRightByRoleAndModule(string roleId, string moduleId)
        {

            var SysRightList = from a in this.db.SysModuleOperate
                               join b_1 in (from ca in this.db.SysRightOperate
                                            from cb in this.db.SysRight
                                            from c in this.db.SysModuleOperate
                                            where (from d in this.db.SysRight where d.RoleId == roleId && d.ModuleId == moduleId select d.Id).Contains(ca.RightId)
                                            && ca.RightId == cb.Id && cb.ModuleId == c.ModuleId && ca.KeyCode == c.KeyCode
                                            select new { c.Id, ca.IsValid }
                                            ) on a.Id equals b_1.Id into joinb
                               from b in joinb.DefaultIfEmpty()
                               where a.ModuleId == moduleId
                               select new P_Sys_GetRightByRoleAndModule_Result
                               {
                                   Id = a.Id,
                                   Name = a.Name,
                                   KeyCode = a.KeyCode,
                                   ModuleId = a.ModuleId,
                                   Sort = a.Sort,
                                   isvalid = (b == null ? false : b.IsValid),
                                   RightId = roleId + moduleId
                               };


            return SysRightList.ToList();
        }

    }
}
